/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package knowledgebase.service;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import knowledgebase.model.DocumentBlock;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author eugene
 */
public class ArticleFormatConverter {

    private final String CLASS_ATTR = "class=";
    private final String[][] tags = {{"div", "section"},
                                     {"p", "para"},
                                     {"h3", "title"}};

    public String convertHTMLToXML(String html) {
        return replaceTags(html, 0);
    }

    private String replaceTags(String html, int mode) {
        if (mode == 0) {
            for (int i = 0; i < tags.length; i++) {
                html = html.replaceAll("<" + tags[i][0], "<" + tags[i][1]).replace("</" + tags[i][0], "</" + tags[i][1]);
            }
        } else {
            for (int i = 0; i < tags.length; i++) {
                html = html.replaceAll("<" + tags[i][1], "<" + tags[i][0]).replace("</" + tags[i][1], "</" + tags[i][0]);
            }
        }
        return html;
    }
    
    public String convertXMLtoHTML(String xml) {
        return replaceTags(xml, 1);
    }

    public List<DocumentBlock> sort(List<DocumentBlock> blocks) {
        Collections.sort(blocks, new DocBlockCpmparator());
        Map<String, DocumentBlock> sortedBlocksMap = new HashMap<String, DocumentBlock>(blocks.size());
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader r = null;
        for (DocumentBlock block : blocks) {
            try {
                r = factory.createXMLStreamReader(new StringReader(block.getData()));
                r.nextTag();
                String id = r.getAttributeValue(null, "id");
                String parentId = r.getAttributeValue(null, "parent");
                sortedBlocksMap.put(id, block);
                DocumentBlock parentBlock = sortedBlocksMap.get(parentId);
                //if parent block exists then add current block into the parent
                //and remove current block from map
                if (parentBlock != null) {
                    parentBlock.setData(parentBlock.getData().replaceAll("</div>", block.getData() + "</div>"));
                    sortedBlocksMap.put(parentId, parentBlock);
                    sortedBlocksMap.remove(id);
                } else if(parentId != null) {
                    sortedBlocksMap.remove(id);
                }
            } catch (XMLStreamException ex) {
                Logger.getLogger(ArticleFormatConverter.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        List<DocumentBlock> result = new ArrayList<DocumentBlock>(sortedBlocksMap.values());
        Collections.sort(result, new DocBlockCpmparator());
        return result;
    }
}
